var parentCenterTabs,_treeDatagrid,dialogAddRow,formAddRow,pid,id,text,number;

function addRow(o) {
	if (o && o != undefined) {
		_treeDatagrid.treegrid('unselectAll');
		_treeDatagrid.treegrid('select', o);
	}
	addRowHandler();
}

function addRowHandler() {
	var node = _treeDatagrid.treegrid('getSelections');
	if (node && node.length > 0) {
		if (node.length > 1) {
			$.messager.alert('提示', '您选择了多个行政区,只能选择一个行政区！', 'warning');
			_treeDatagrid.treegrid('unselectAll');
		} else {
			pid.combotree('setValue', node[0].id).combotree('setText', node[0].text).combotree('disable');
			id.val(node[0].id);
			text.val('');
			number.val('');
			dialogAddRow.dialog({title:'添加行政区',iconCls:'icon-add',buttons:[{text:'添加',handler:dialogEnterHandler},{text:'关闭',handler:function(){dialogAddRow.dialog('close');_treeDatagrid.treegrid('reload');}}]});
			dialogAddRow.dialog('open');
		}
	} else {
		$.messager.alert('提示', '您没有选择上级行政区,请选择上级行政区！', 'warning');
	}
}

function deleteRow(o) {
	if (o && o != undefined) {
		_treeDatagrid.treegrid('unselectAll');
		_treeDatagrid.treegrid('select', o);
	}
	deleteRowHandler();
}

function deleteRowHandler() {
	var node = _treeDatagrid.treegrid('getSelections');
	if (node && node.length > 0) {
		$.messager.confirm('请确认', '您确实要删除当前所有选择的记录吗？\n注意：此操作将删除该行政区下的所有子行政区！', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < node.length; i++) {
					if (node[i].id == 1) {
						$.messager.alert('警告', "对不起,您不能删除根节点!", 'warning');
						return;
					}
					ids.push(node[i].id);
				}
				$.post(sy.bp()+"/admin/citys/delete", {ids:ids.join(",")}, function(data) {
					if (data.result==1) {
						$.messager.show({msg:data.msg,title:'系统提示'});
						_treeDatagrid.treegrid('unselectAll');
						_treeDatagrid.treegrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
}

function updateRow(o) {
	if (o && o != undefined) {
		_treeDatagrid.treegrid('unselectAll');
		_treeDatagrid.treegrid('select', o);
	}
	updateRowHandler();
}

function updateRowHandler() {
	var node = _treeDatagrid.treegrid('getSelections');
	if (node && node.length > 0) {
		if (node.length > 1) {
			$.messager.alert('提示', '您选择了多个行政区,只能选择一个行政区！', 'warning');
			_treeDatagrid.treegrid('unselectAll');
		} else {
			if (node[0].id == 1) {
				$.messager.alert('警告', "对不起,您不能更新根节点!", 'warning');
				return;
			}
			pid.combotree('setValue', node[0].pid).combotree('setText', node[0].ptext).combotree('enable');
			id.val(node[0].id);
			text.val(node[0].text);
			number.val(node[0].number);
			dialogAddRow.dialog({title:'更新行政区',iconCls:'icon-edit',buttons:[{text:'更新',handler:dialogEnterHandler}]});
			dialogAddRow.dialog('open');
		}
	} else {
		$.messager.alert('提示', '您没有选择行政区,请选择！', 'warning');
	}
}

function reloadRow() {
	_treeDatagrid.treegrid('reload');
}

function unselectRow() {
	_treeDatagrid.treegrid('unselectAll');
}

function dialogEnterHandler() {
	var url = sy.bp() + '/admin/citys/';
	if (formAddRow.form('validate')) {
		if (id.val() == pid.combotree('getValue')) {
			$.post(url + 'add', {"number":number.val(),"text":text.val(),"pid":id.val()}, function(data){
				if(data && data.result ==1) {
					$.messager.show({title:'系统提示', msg:data.msg, timeout:2000, showType:'slide'});
					text.val('');
					number.val('');
					formAddRow.form('validate');
				} else {
					$.messager.alert('错误提示', data.msg);
					formAddRow.form('validate');
				}
			},"json");
		} else {
			$.post(url + 'update', formAddRow.serialize(), function(data){
				if(data && data.result ==1) {
					dialogAddRow.dialog('close');
					$.messager.show({title:'系统提示', msg:data.msg, timeout:2000, showType:'slide'});
					_treeDatagrid.treegrid('unselectAll');
					_treeDatagrid.treegrid('reload');
				} else {
					$.messager.alert('错误提示', data.msg);
				}
			},"json");
		};
	};
}

/**开通城市*/
function approvedRow(o) {
	if (o) {
		_treeDatagrid.treegrid('select', o);
	}
	approvedRowHandler();
}

/**开通城市*/
function approvedRowHandler() {
	var node = _treeDatagrid.treegrid('getSelections');
	if (node && node.length > 0) {
		$.messager.confirm('请确认', '您确实要改变当前选择的城市的开通状态吗？注意：此操作将改变城市下所有子行政区域的开通状态！！！', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < node.length; i++) {
					if (node[i].id == 1) {
						$.messager.alert('警告', "对不起,您不能开通根节点!", 'warning');
						return;
					}
					ids.push(node[i].id);
				}
				var status = 0;
				if (node[0].status == 0) {
					status = 1;
				}
				$.post(sy.bp()+"/admin/citys/updateApproved", {ids:ids.join(","), status:status}, function(data) {
					if (data.result==1) {
						$.messager.show({msg:data.msg,title:'系统提示'});
						_treeDatagrid.treegrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要开通的城市!', 'error');
	}
}

/**轮播管理*/
function bannerRow(o) {
	if (o) {
		_treeDatagrid.treegrid('select', o);
	}
	bannerRowHandler();
}

/**轮播管理*/
function bannerRowHandler() {
	var node = _treeDatagrid.treegrid('getSelections');
	if (node && node.length == 1) {
		if (node[0].id > 1) {
			sy.addTab(sy.bp()+"/admin/banner/list?flag=1&objId="+node[0].id, parentCenterTabs, node[0].text+"轮播管理")
		} else {
			$.messager.alert('提示', '无法对根机构进行轮播管理,请选择机构!', 'error');
		}
	} else {
		$.messager.alert('提示', '每次只能打开一个机构的轮播管理,请选择机构!', 'error');
	}
}

$(function() {
	parentCenterTabs = parent.centerTabs;
	id = $('#id');
	text = $('#text');
	number = $('#number');
	
	/*初始化添加行政区对话框中的form*/
	formAddRow = $('#formAddRow').form({});
	
	/*设置pid.combotree点击输入框弹出面板*/
	pid = $('#pid').combotree('activeTextArrow');
	
	/*初始化添加行政区对话框*/
	dialogAddRow = $('#dialogAddRow').show().dialog({resizable:false,closed:true,closable:true,modal:true});
	
	/*绑定添加行政区对话框回车事件*/
	dialogAddRow.keydown(function(event){
		if(event.keyCode==13){
			dialogEnterHandler();
		}
	});
	
	/*初始化行政区treegrid*/
	_treeDatagrid = $('#_treeDatagrid').treegrid({
		url : sy.bp() + '/admin/citys/listTree',
	    idField:'id',
	    treeField:'text',
	    fit : true,
		autoRowHeight : true,
		striped : true,
		rownumbers : true,
		singleSelect : true,
		fitColumns : true,
		nowrap : true,
		border : false,
		toolbar: [
				{text:'增加',iconCls: 'icon-add',handler: addRowHandler},'-',
				{text:'批量删除',iconCls: 'icon-remove',handler: deleteRowHandler},'-',
				{text:'修改',iconCls: 'icon-edit',handler: updateRowHandler},'-',
				{text:'取消选中',iconCls: 'icon-redo',handler: unselectRow},'-',
				{text:'刷新',iconCls: 'icon-reload',handler: reloadRow},'-',
				{text:'开通城市',iconCls: 'icon-page_white_paintbrush',handler: approvedRowHandler},'-',
				{text:'轮播管理',iconCls: 'icon-feed',handler: bannerRowHandler},'-'
				],   
		columns:[[
		          {field:'text',title:'行政区名称',width:200,align:'left'},
		          {field:'code',title:'行政区编号',width:100,align:'center'},
		          {field:'quanpin',title:'全拼',width:100,align:'left'},
		          {field:'jianpin',title:'简拼',width:100,align:'left'},
		          {field:'status',title:'状态',width:100,align:'center',
		        	  formatter:function(value, row) {
		        		  if (value==1) {
		        			  return '<span style="color:green">开通</span>';
		        		  } 
		        		  return '<span style="color:red">未开通</span>'; 
		        	  }
		          }
		         ]],
	    onLoadSuccess : function(data) {
			_treeDatagrid.treegrid('expand', 1);
			$.messager.progress('close');
		},
		onDblClickRow : function(row){
			if ('open' == row.state) {
				updateRow(row.id);
			} else {
				_treeDatagrid.treegrid('toggle', row.id);
			}
			
		},
		onContextMenu : function(e, row){
			_treeDatagrid.treegrid('unselectAll');
			_treeDatagrid.treegrid('select', row.id);
			e.preventDefault();
			$('#treeridContextMenu').menu('show', {left:e.pageX,top:e.pageY});
		}
	});
});